/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.common.util;

import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;

public class ArrayUtil {

    public static void main(String[] args) {
        int[][] sheet1 = ArrayUtil.getSheet2();
        for (int i = 0; i < 16; i++) {
            System.out.println(Arrays.toString(sheet1[i]));
        }
        if (ArrayUtils.indexOf(sheet1[2], 7,0) > -1){
            System.out.println("存在 ~~~~");
        }
        System.out.println("初始化数组~~~~");
    }

    /**
     * 小计表格
     * @return
     */
    public static void sumCells(int[][] sheet,int row,int column,int start) {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j ++) {
                if (i > start){
                    sheet[i][column] += sheet[i][j]; // 第二行开始统计
                    sheet[row][j] += sheet[i][j];
                }
            }
            if (i > start) {
                sheet[row][column] += sheet[i][column];
            }
        }
    }

    /**
     * 尾行相乘计算结果
     * @param sheet
     * @param row
     * @param column
     * @param start
     * @return
     */
    public static Integer statisticsTotal(int[][] sheet, int row, int column,int column1, int start) {
        Integer total = 0;
        for (int i = 0; i < row; i++) {
            int length = sheet[i].length;
            for (int j = 0; j < length; j++) {
                if (j < 11){
                    Integer value= sheet[i][j]; // 当前数量
                    Integer value1= sheet[i][column1]; // 单位积分
                    Integer coefficient = sheet[14][j]; // 系数
                    Integer mul = ArithHelper.mul(coefficient,ArithHelper.mul(value, value1)); // 第二行开始统计
                    sheet[15][j] = sheet[15][j] + mul;
                    total += mul;
                }
            }
        }
        return total;
    }

    /**
     * 报告表格
     * @return
     */
    public static int[][] getSheet2() {
        // 初始化一个二维数组
        int[][] sheet = initSheet(14,6);
        return sheet;
    }

    /**
     * 初始化 表格
     * @param r
     * @param c
     * @return
     */
    public static int[][] initSheet(int r,int c) {
        int[][] sheet = new int[r][c];
        for (int i = 0; i < r ;i++) {
            for (int j = 0; j < c; j++) {
                sheet[i][j] = 0;
            }
        }
        return sheet;
    }

    public static JSONArray initJSONArray(Integer size) {
        JSONArray jsonArray = new JSONArray(size);
        for (int i = 0; i < size; i++) {
            jsonArray.set(i,0);
        }
        return jsonArray;
    }
    /**
     * 初始化 表格
     * @param r
     * @param c
     * @return
     */
    public static Object[][] initObjectSheet(int r,int c) {
        Object[][] sheet = new Object[r][c];
        for (int i = 0; i < r ;i++) {
            for (int j = 0; j < c; j++) {
                sheet[i][j] = 0;
            }
        }
        return sheet;
    }

    public static int[] initArray(int i, int i1) {
        int[] arr = new int[i];
        for (int j = 0; j < i; j++) {
            arr[j] = i1;
        }
        return arr;

    }
}
